package hci.project.reader1;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.util.Log;
import android.widget.Button;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Locale;
import android.widget.LinearLayout;

public class ReaderByNonSpeech extends Activity implements
		TextToSpeech.OnInitListener {
	/** Called when the activity is first created. */

	private static final String TAG = "TTSdemo";

	private LinearLayout layout;
	private TextToSpeech mTts;
	private Button backButton;
	private Button nextButton;
	private ImageView image;
	private TextView title;
	private TextView myText1;
	private TextView myText2;
	private TextView myText3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mTts = new TextToSpeech(this, this);

		layout = (LinearLayout) findViewById(R.id.linearLayout);
		backButton = (Button) findViewById(R.id.backbutton);
		nextButton = (Button) findViewById(R.id.nextbutton);
		image = (ImageView) findViewById(R.id.imageView);
		title = (TextView) findViewById(R.id.textView1);
		myText1 = (TextView) findViewById(R.id.textView2);
		myText2 = (TextView) findViewById(R.id.textView3);
		myText3 = (TextView) findViewById(R.id.textView4);
		
		
		title.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(title.getText().toString());
				return false;
			}
		});
		myText1.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(myText1.getText().toString());
				return false;
			}
		});
		myText2.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(myText2.getText().toString());
				return false;
			}
		});
		myText3.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(myText3.getText().toString());
				return false;
			}
		});
		
		nextButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(nextButton.getText().toString()+"button");
				return false;
			}
		});
		backButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(backButton.getText().toString()+"button");
				return false;
			}
		});
		image.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say("image");
				return false;
			}
		});
		layout.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				say(event.getX() + "," + event.getY());
				return false;
			}
		});

	}

	@Override
	public void onDestroy() {
		if (mTts != null) {
			mTts.stop();
			mTts.shutdown();
		}
		super.onDestroy();
	}

	// implements TextToSpeech.OnInitListener.
	public void onInit(int status) {
		// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
		if (status == TextToSpeech.SUCCESS) {
			// Set preferred language to US english.
			// Note that a language may not be available, and the result will
			// indicate this.
			int result = mTts.setLanguage(Locale.US);
			// Try this someday for some interesting results.
			// int result mTts.setLanguage(Locale.FRANCE);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				// Lanuage data is missing or the language is not supported.
				Log.e(TAG, "Language is not available.");
			} else {
				// Check the documentation for other possible result codes.
				// For example, the language may be available for the locale,
				// but not for the specified country and variant.

				// The TTS engine has been successfully initialized.
				// Allow the user to press the button for the app to speak
				// again.
				// backButton.setEnabled(true);
				// Greet the user.
				say(title.getText().toString());
			}
		} else {
			// Initialization failed.
			Log.e(TAG, "Could not initialize TextToSpeech.");
		}
	}

	private void say(String input) {
		mTts.speak(input, TextToSpeech.QUEUE_FLUSH, null);
	}
}